<template>
	<view class="warp">
		<!-- #ifdef APP-PLUS -->
		<view class="title-contents">
			<view class="top-view status"></view>
		</view>
		<!-- #endif -->
		<view class="login-title">
			<view class="logo">
				<image src="/static/images/common/logo.png" mode=""></image>
			</view>
		</view>
		<view class="login-phone form-wapper">
			<view class="phone input-wapper">
				<view class="lside"><text class="iconfont icon-shouji"></text></view>
				<view class="mobile rside"><input v-model="username" placeholder-class="input-placeholder" type="number"
						placeholder="请输入手机号" /></view>
			</view>
			<view class="code input-wapper">
				<view class="lside"><text class="iconfont icon-mima2"></text></view>
				<view class="rside"><input v-model="password" placeholder-class="input-placeholder" type="password"
						maxlength="12" placeholder="请输入密码(6-12位) " /></view>
			</view>
			<!-- <view class="paw"><view class="btn" >验证码登录</view></view> -->
		</view>

		<button type="primary" class="login-btn" :loading="submit_loading" @click="login">登录</button>
		<view class="reg-paw">
			<text class="active" @click="navTo('/pages/public/reg')">注册账号</text>
			<text>忘记密码</text>
		</view>
		<!-- #ifdef APP-PLUS -->
		<view class="quick">
			<view class="quick-title">
				<text class="line"></text>
				<text class="txt">快捷登陆</text>
				<text class="line"></text>
			</view>
			<view class="sanfang">
				<image src="../../static/temp/share_wechat.png" mode=""></image>
			</view>
		</view>
		<!-- #endif -->
	</view>
</template>
<script>
	import {
		loginApi
	} from "@/api/user.js"
	export default {
		data() {
			return {
				username: '',
				password: '123456',
				submit_loading: false
			}
		},
		onLoad(options) {
			this.username = options.username;
			console.log(this.username)
		},
		methods: {
			login() {
				let {
					username,
					password
				} = this;

				if (!/^1[3-9]\d{9}$/.test(username)) {
					// this.$toast('手机号码格式错误');
					return;
				}
				if (!/^\w{6,12}$/.test(password)) {
					// this.$toast('密码格式错误');
					return;
				}
				loginApi({
					username,
					password
				}).then(res => {
					console.log(res.data)
					this.submit_loading = false;
					if (res.data.code == 0) {
						let {
							token,
							tokenExpired,
							uid,
							username,
							userInfo
						} = res.data;
						uni.setStorageSync('token', token)
						uni.setStorageSync('tokenExpired', tokenExpired)
						uni.setStorageSync('uid', uid)
						//返回上一级
						if(this.username = username){
							uni.navigateBack({
								delta:3
							})
						}else{
							uni.navigateBack({
								delta:1
							})
						}
					}
				})
			}
		}
	}
</script>
<style lang="scss">
	@import "login.scss"
</style>
